
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author: 子龙
 */


/**
 * 让默认创建小根堆变为大根堆
 * \    public static int compare(int x, int y) {
 *         return (x < y) ? -1 : ((x == y) ? 0 : 1);
 *     }
 */
 class IntCmp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}


public class PriorityQueueTest {

    /**
     * topKey问题(前k个最小)，创建大根堆
     * @param arr
     * @param k
     * @return
     */
    public int[] smallestK1(int[] arr, int k){
        int[] res = new int[k];
        if(arr == null || k==0){
            return res;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp());
        for(int i=0; i<k; i++){
            priorityQueue.offer(arr[i]);
        }
        for(int i=k; i<arr.length; i++){
            int peekVal=priorityQueue.peek();
            if(arr[i]<peekVal){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }


        for(int i=0; i<k; i++){
            res[i] = priorityQueue.poll();
        }

        return res;

    }


    /**
     * topKey问题(前k个最小)，创建小根堆
     * @param arr
     * @param k
     * @return
     */
    public int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }

        int [] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }

        return ret;
    }




    public static void main(String[] args) {

        //默认创建小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(1);
        priorityQueue.offer(2);
        priorityQueue.offer(3);


        System.out.println(priorityQueue.peek());;
    }
}